package com.geeaks.learn.concurrent.sermaphore;

public class ResourceUser implements Runnable {
	
	private ResourceManage resourceManage;
	
	private int userId;
	
	public ResourceUser(ResourceManage resourceManage, int userId) {
		this.resourceManage = resourceManage;
		this.userId = userId;
	}
	
	public void run() {
		System.out.println(userId + ":准备使用资源");
		try {
			resourceManage.useResource(userId);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println(userId + ":使用资源完毕");
	}
	
	public static void main(String[] args) {
		ResourceManage resourceManage = new ResourceManage();
		Thread[] threads = new Thread[10];
		for (int i = 0; i < 10; i++) {
			Thread thread = new Thread(new ResourceUser(resourceManage, i));// 创建多个资源使用者
			threads[i] = thread;
			try {
				thread.start();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}